
package com.kugou.game.sdk.ui.activity;

import com.kugou.game.sdk.adapter.CostRecordsBaseAdater;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.base.BaseCommonTitleFragmentActivity;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.PayRecord;
import com.kugou.game.sdk.interfaces.OnLoadPayRecordListener;
import com.kugou.game.sdk.statistics.StatCmd;
import com.kugou.game.sdk.statistics.StaticsManager;
import com.kugou.game.sdk.ui.widget.TipsLayout;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * 消费记录界面
 * 
 * @author ljc
 * @since 2013-12-24 下午2:48:13
 */
public class ChargeHistoryActivity extends BaseCommonTitleFragmentActivity {

    private final int MSG_BACK_REQUEST_PAY_RECORD = 1;

    private final int MSG_UI_REQUEST_START = 2;

    private final int MSG_UI_REQUEST_PAY_RECORD_SUCCESS = 3;

    private final int MSG_UI_REQUEST_PAY_RECORD_FAILED = 4;

    private Context context;

    private View layoutChargeHistory;

    private ListView listView;

    private TipsLayout mTipsLayout;

    private TextView tvLoadingWrong;

    private CostRecordsBaseAdater adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        int layoutId = Rx.layout.kg_activity_charge_history;
        setContentView(layoutId);
        // 设置标题
        setCommonTitle(Rx.string.kg_kugou_game_charge_history);
        // 设置View界面
        initView();
        sendEmptyBackgroundMessage(MSG_BACK_REQUEST_PAY_RECORD);

        // 发送统计-进入我的消费记录
        StaticsManager.sendCommonStatis(StatCmd.CMD_BI_ENTER_COSTRECORD);

    }

    private void initView() {
        layoutChargeHistory = findViewById(Rx.id.kg_layout_charge_history);
        listView = (ListView) findViewById(Rx.id.kg_listView);
        adapter = new CostRecordsBaseAdater(context, new ArrayList<PayRecord>());
        View paddingView = new View(this);
        paddingView.setLayoutParams(new AbsListView.LayoutParams(
                AbsListView.LayoutParams.MATCH_PARENT, AppUtil.dip2px(this, 10)));
        listView.addHeaderView(paddingView);
        listView.setAdapter(adapter);

        // tvLoadingWrong = (TextView) findViewById(Rx.id.tv_loading_wrong);
        mTipsLayout = (TipsLayout) findViewById(Rx.id.kg_tips_layout);
        mTipsLayout.setLoadingText("");
        mTipsLayout.setLoadingViewBackground(Rx.drawable.kg_gamecenter_loading);
        mTipsLayout.setOnRefreshButtonClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                sendEmptyBackgroundMessage(MSG_BACK_REQUEST_PAY_RECORD);
            }
        });
    }

    private void showLoadingView() {
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_LOADING);
        layoutChargeHistory.setVisibility(View.GONE);
    }

    private void showLayoutChargeHistory() {
        mTipsLayout.setVisibility(View.GONE);
        layoutChargeHistory.setVisibility(View.VISIBLE);
    }

    private void showLayoutResult() {
        layoutChargeHistory.setVisibility(View.GONE);
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_FAILE);
    }

    private void showLayoutEmpty() {
        layoutChargeHistory.setVisibility(View.GONE);
        mTipsLayout.setVisibility(View.VISIBLE);
        mTipsLayout.show(TipsLayout.TYPE_EMPTY_CONTENT);
    }

    @Override
    public void handleBackgroundMessage(Message msg) {
        super.handleBackgroundMessage(msg);
        switch (msg.what) {
            case MSG_BACK_REQUEST_PAY_RECORD:
                sendEmptyUiMessage(MSG_UI_REQUEST_START);
                getPayRecordList();
                break;

            default:
                break;
        }
    }

    @Override
    public void handleUiMessage(Message msg) {
        super.handleUiMessage(msg);
        switch (msg.what) {
            case MSG_UI_REQUEST_START:
                showLoadingView();
                break;
            case MSG_UI_REQUEST_PAY_RECORD_SUCCESS:
                if (msg.obj != null) {
                    ArrayList<PayRecord> payRecords = (ArrayList<PayRecord>) msg.obj;
                    if (payRecords.size() == 0) {
                        showLayoutEmpty();
                    } else {
                        showLayoutChargeHistory();
                        adapter.refreshData(payRecords);
                        if (payRecords.size() >= 10) {
                            listView.addFooterView(getFooterView());
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
            case MSG_UI_REQUEST_PAY_RECORD_FAILED:
                showLayoutResult();
                // tvLoadingWrong.setText((String) msg.obj);
                break;

            default:
                break;
        }
    }

    private void getPayRecordList() {
        User user = UserManager.getInstance().getCurrentUserInternal();
        String userName = user.getAccountName();
        UserManager.getInstance().getPayRecordList(userName, onLoadPayRecordListener);
    }

    OnLoadPayRecordListener onLoadPayRecordListener = new OnLoadPayRecordListener() {

        @Override
        public void onSuccess(ArrayList<PayRecord> payRecordList) {
            Message msg = new Message();
            msg.what = MSG_UI_REQUEST_PAY_RECORD_SUCCESS;
            msg.obj = payRecordList;
            sendUiMessage(msg);
        }

        @Override
        public void onFailed(String errorMsg) {
            Message msg = new Message();
            msg.what = MSG_UI_REQUEST_PAY_RECORD_FAILED;
            msg.obj = errorMsg;
            sendUiMessage(msg);
        }
    };

    private View getFooterView() {
        View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(Rx.layout.kg_layout_footer_loading, null);
        TextView info = (TextView) footerView.findViewById(Rx.id.kg_loading_tips);
        info.setText("为保障您的数据安全，更多消费记录联系客服查看");
        footerView.findViewById(Rx.id.kg_loading_progress_bar).setVisibility(View.GONE);
        return footerView;
    }

}
